package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.entity.CementingConstructData;
import com.b511.swpu.cementbackend.repository.CementingConstructDataRepository;
import com.b511.swpu.cementbackend.service.CementingConstructDataService;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Api(value = "固井总体设计", tags = {"固井总体设计"})
@RestController
@RequestMapping("/cementing_construct_data")
public class CementingConstructDataController {
    private final CementingConstructDataService cementingConstructDataService;
    private final CementingConstructDataRepository cementingConstructDataRepository;

    public CementingConstructDataController(CementingConstructDataService cementingConstructDataService,
                                            CementingConstructDataRepository cementingConstructDataRepository) {
        this.cementingConstructDataService = cementingConstructDataService;
        this.cementingConstructDataRepository = cementingConstructDataRepository;
    }

    @GetMapping("/{num}/{size}")
    public R getCementingConstructDataByCondition(@PathVariable int num,
                                               @PathVariable int size) {
        return cementingConstructDataService.getCementingConstructData( PageRequest.of(num, size));
    }

    @PutMapping("/{id}")
    public R updateCementingConstructData(@PathVariable Long id, @RequestBody CementingConstructData updateCementingConstructData) {
        return cementingConstructDataService.updateCementingConstructData(id, updateCementingConstructData);
    }

    @DeleteMapping("/{id}")
    @Transactional
    public R deleteCementingConstructDataByCondition(@PathVariable Long id) {
        cementingConstructDataRepository.deleteById(id);
        return R.ok();
    }
}
